package com.example.service.impl;

import com.example.entity.Course;
import com.example.entity.example.CourseExample;
import com.example.mapper.CourseMapper;
import com.example.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
@Transactional(readOnly = true)
public class CourseServiceImpl implements CourseService {

    @Autowired(required = false)
    CourseMapper courseMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void add(Course course) {
        courseMapper.insert(course);
    }

    @Override
    public Course getById(String id) {
        return courseMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void updateById(Course course) {
        courseMapper.updateByPrimaryKey(course);
    }

    @Override
    @Transactional
    public void updateSelectiveById(Course course) {
        courseMapper.updateByPrimaryKeySelective(course);
    }

    @Override
    @Transactional
    public void removeById(String id) {
        courseMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Course> getByExampleWithLimit(CourseExample example, Integer page, Integer limit) {
        int begin = (page - 1) * limit;
        return courseMapper.selectByExampleWithLimit(example, begin, limit);
    }

    @Override
    public long getCountByExample(CourseExample example) {
        return courseMapper.countByExample(example);
    }

    @Override
    @Transactional
    public void selection(String courseId, String studentId) {
        courseMapper.insertSelectionCourse(courseId, studentId, new Date());
    }

    @Override
    @Transactional
    public void unSelection(String courseId, String studentId) {
        courseMapper.deleteSelectionCourse(courseId, studentId);
    }

    @Override
    public List<Course> getByTeacherId(String teacherId) {
        return courseMapper.selectByTeacherId(teacherId);
    }

}
